package com.wuchao.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuchao.entity.User;
import com.wuchao.exception.BusinessException;
import com.wuchao.response.R;
import com.wuchao.response.ResultCode;
import com.wuchao.service.LoginLogService;
import com.wuchao.service.UserService;
import com.wuchao.utils.UploadDAO;
import com.wuchao.utils.UploadDataListener;
import com.wuchao.vo.UserVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author wuchao
 * @since 2023-12-12
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Resource
    UserService userService;
    @GetMapping("test")
    public R test(int id){
        User byId = userService.getById(id);
        if(byId==null){
            throw new BusinessException(ResultCode.USER_NOT_FIND.getCode(),
                    ResultCode.USER_NOT_FIND.getMessage());
        }
        return R.ok().data("user",byId);
    }


    @ApiOperation("双表联查分页带uservo条件模糊查询")
    @PostMapping("findUserList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页"),
            @ApiImplicitParam(name = "pageSize",value = "每页展示数")
    })//    Page<Entry> page = new Page<>(2,3);
    public R findUserList(
            @RequestParam(value = "currentPage",required = false,
                    defaultValue = "1")Integer currentPage, @RequestParam(value = "pageSize",required = false,
            defaultValue = "5")Integer pageSize, @RequestBody(required = false) UserVO userVO){
        Page<User> userPage = new Page<User>(currentPage,pageSize);
        Page<User> userList = userService.findUserList(userPage, userVO);
        return  R.ok().data("userList",userList);
    }

    //
    @ApiOperation(value = "导出用户信息")
    @PostMapping(value = "/export")//user/export
    //因为不能返回，返回单个result.ok也是报错，
    public void exportUserExcel(HttpServletResponse response, @RequestBody(required = false) UserVO userVO) throws IOException {
        int count = userService.count();
        Page<User> userPage = new Page<User>(1,count);
        Page<User> userList = userService.findUserList(userPage, userVO);
        List<User> records = userList.getRecords();
        userService.exportUserExcel(response,records);
    }

    //导入
    @Autowired   private UploadDAO uploadDAO;

    @PostMapping("/import")
//    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), User.class, new UploadDataListener(uploadDAO)).sheet().doRead();
        return "success";
    }
    @Resource
    LoginLogService loginLogService;
    //
    @ApiOperation(value = "用户登入", notes = "接收参数用户名和密码,登入成功后,返回JWTToken")
    @PostMapping("/login")
    public  R login(String username, String password, HttpServletRequest request){
        String token = userService.login(username, password);
        loginLogService.addLoginLog(request);
        System.out.println(token);
        return  R.ok().data("token",token);//送往前端
    }
}

